package com.server.rule.config;

import com.server.rule.constant.RuleEngineConstant;
import com.server.rule.factory.NettyFactFactory;
import com.server.rule.factory.NettyMvelRuleFactory;
import com.server.rule.factory.NettyRuleFactory;
import com.server.rule.factory.RuleEngineFactory;
import com.server.rule.loader.yml.YmlRuleReader;
import com.server.rule.protocolInfo.Message;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.mvel.MVELRuleFactory;

import java.util.List;

/**
 * Description: This is a sample class.
 * @Author Simon
 */
@Slf4j
public class RuleConfiguration {

    private static final String YML_HOME = RuleEngineConstant.BASE_YML_HOME;
    private RulesEngine rulesEngine;
    private Facts factRepository;
    private Rules ruleRepository;
    private MVELRuleFactory mvelRuleFactory;

    void initFactory() {
        this.rulesEngine = RuleEngineFactory.getInstance();
        this.factRepository = NettyFactFactory.getInstance();
        this.ruleRepository = NettyRuleFactory.getInstance();
        this.mvelRuleFactory = NettyMvelRuleFactory.getInstance();
    }

    void initRule() {
        YmlRuleReader ymlRuleReader = new YmlRuleReader();
        ymlRuleReader.setMvelRuleFactory(mvelRuleFactory);
        List<Rule> ruleList = ymlRuleReader.loadRulesByPath(YML_HOME);
        ruleList.forEach(rule->ruleRepository.register(rule));
    }

    public void initConfiguration() {
        initFactory();
        initRule();
    }

    public RulesEngine getRulesEngine() {
        return rulesEngine;
    }

    public void setRulesEngine(RulesEngine rulesEngine) {
        this.rulesEngine = rulesEngine;
    }

    public Facts getFactRepository() {
        return factRepository;
    }

    public void setFactRepository(Facts factRepository) {
        this.factRepository = factRepository;
    }

    public Rules getRuleRepository() {
        return ruleRepository;
    }

    public void setRuleRepository(Rules ruleRepository) {
        this.ruleRepository = ruleRepository;
    }
}
